package com.sg.business.vault.handler;

import java.util.List;
import java.util.Map;

import org.eclipse.core.commands.Command;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;

import com.mobnut.db.model.IContext;
import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.Document;
import com.sg.business.model.Folder;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.command.AbstractNavigatorHandler;
import com.sg.widgets.part.CurrentAccountContext;
import com.sg.widgets.viewer.ViewerControl;

public class ReleasedDocumentForFolder extends AbstractNavigatorHandler {

	@Override
	protected void execute(PrimaryObject selected, IWorkbenchPart part,
			ViewerControl vc, Command command, Map<String, Object> parameters,
			IStructuredSelection selection) {
		try {
			IContext context = new CurrentAccountContext();
			if (selected instanceof Folder) {
				Folder folder = (Folder) selected;
				List<Document> documents = folder.getDocuments();
				for (Document doc : documents) {
					if (doc.checkLifeCycle(Document.STATUS_WORKING_ID)) {
						doc.doSetLifeCycleStatus(context,
								Document.STATUS_RELEASED_ID);
					}
				}
			} else if (selected instanceof Document) {
				Document doc = (Document) selected;
				if (doc.checkLifeCycle(Document.STATUS_WORKING_ID)) {
					doc.doSetLifeCycleStatus(context,
							Document.STATUS_RELEASED_ID);
				}
			}
		} catch (Exception e) {
			MessageUtil.showToast(e);
		}
	}

}
